import React, { useState, useEffect } from 'react';


const AuthRedirect = () => {

    const initLoaded = (event) => {
        // console.log('handleLoaded===', event);
        localStorage.setItem("url", window.location.href);

        // 获取url中的授权码
        // const code = getQueryVariable(window.location.href, "code");
        // console.log('code===', code);


        let urlSearch = new URLSearchParams(window.location.search)
        let code = urlSearch.get("code")
        let state = urlSearch.get("state")


        let hash = window.location.hash.substring(1)
        if (hash.includes("?")) {
            hash = hash.substring(hash.lastIndexOf("?"))
        }

        if (hash.length > 0) {
            let hashSearch = new URLSearchParams(hash)

            if (!code) {
                code = hashSearch.get("code")
            }
            if (!state) {
                state = hashSearch.get("state")
            }
        }

        // 将授权码放进本地存储中
        if (code && state) {
            localStorage.setItem("authCode", code)
            localStorage.setItem("authState", state)
        }
    }

    useEffect(() => {
        // console.log('init.....')
        initLoaded()


        // Dom 渲染完即可执行 此时图片视频还可能没加载完
        // 页面的全部资源加载完后才会执行 包括 图片 视频等 indow.addEventListener("load")
        // window.addEventListener("load", handleLoaded);
        return () => {
            // window.removeEventListener("load", handleLoaded)
        }
    }, [])

  

    return (
        <>
            <h1>callback</h1>
        </>
    );
}

export default AuthRedirect